package top.kaoshanji.p2ch10a;

import java.util.Arrays;

/**
 * 代码清单-p2ch10a-237页a：策略模式
 * author kaoshanji
 * ^_^
 * create time 2025/3/10 19:04
 */
public class Applicator {

    // 根据传递的参数对象来表现出不同的行为
    // 方法包含了算法的固定部分，而策略（Processor）包含了变化的部分
    public static void apply(Processor p, Object s) {
        System.out.println("Using Processor " + p.name());
        System.out.println(p.process(s));
    }

    public static void main(String[] args) {

        String s = "We are such stuff as dreams are made on";

        apply(new Upcase(), s);
        apply(new Downcase(), s);
        apply(new Splitter(), s);

    }

}

class Processor {

    public String name() {
        return getClass().getSimpleName();
    }

    public Object process(Object input) {
        return input;
    }

}

class Upcase extends Processor {

    @Override
    public Object process(Object input) {
        return ((String)input).toUpperCase();
    }
}

class Downcase extends Processor {

    @Override
    public Object process(Object input) {
        return ((String)input).toLowerCase();
    }
}

class Splitter extends Processor {

    @Override
    public Object process(Object input) {
        // split() 方法把一个字符串分为几个部分
        return Arrays.toString(((String)input).split(" "));
    }
}

